//
//  Gift_TopicsView.m
//  MindBox
//
//  Created by MS on 16-1-3.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Gift_TopicsView.h"
#import "UIView+Frame.h"
#import "UIImageView+WebCache.h"
@interface Gift_TopicsView ()

@property (nonatomic,strong) NSMutableArray * imageViewArr;

@end

@implementation Gift_TopicsView

-(NSMutableArray *)imageViewArr
{
    if (_imageViewArr==nil) {
        _imageViewArr = [[NSMutableArray alloc] init];
        
    }
    return _imageViewArr;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        for (int i =0; i<4; i++) {
            
            UIImageView * imageView = [[UIImageView alloc]init];
            [self addSubview:imageView];
            imageView.tag = i;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick:)];
            [imageView addGestureRecognizer:tap];
            
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
        
            [self.imageViewArr addObject:imageView];
        }
        
    }
    return  self;
}


-(void)setGift_topic:(NSArray *)gift_topic
{
    _gift_topic = gift_topic;
    
    int i=0;
    
    for (Gift_Topics * gift in gift_topic) {
        
        UIImageView * imaV = self.imageViewArr[i];
        [imaV sd_setImageWithURL:[NSURL URLWithString:gift.image_url]];
        
        i++;
    }
    
    
}

-(void)imageViewClick:(UIGestureRecognizer *)tap
{
    NSInteger index =tap.view.tag;
    
    if ([self.delegate respondsToSelector:@selector(gift_TopicsViewDidClick:gift_topic:index:)]) {
        [self.delegate gift_TopicsViewDidClick:self gift_topic:self.gift_topic[index] index:index];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 10;
    CGFloat w =(self.width-30)/2;
    CGFloat h =60;
    int i=0;
    for (UIImageView * imageV in self.imageViewArr) {
        
        int col =i%2;
        int rol =i/2;
        CGFloat x = margin + (margin+w)*col;
        CGFloat y = margin +(margin+h)*rol;
        imageV.frame = CGRectMake(x, y, w, h);
        i++;
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
